<?php

namespace app\admin\controller;

use app\models\{Business, PlatClass, PlatSons, Goods, GoodsSpec, Region, BusinessArea, Compent, User as Users, UserLevel, Order as Orders, OrderGoods, Coupon, Address};
use think\Db;
use think\Validate;

class User extends Base
{

    public function user_level()
    {
        $results = (new UserLevel())->order(['level' => 'desc']);
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['coupon'] = json_decode($result['coupon'], true);
        }
        return view(null, [
            'list' => $results
        ]);
    }

    public function user_level_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['coupon'] = json_encode($data['parpam'], true);
            $res = (new UserLevel())->allowfield(true)->save($data, ['id' => $data['id']]);
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 0, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new UserLevel())->where(['id' => $id])->find();
            if ($row) {
                $row['coupon'] = json_decode($row['coupon'], true);
            }
            return view('', [
                'row' => $row
            ]);
        }
    }

    public function index()
    {
        $results = (new Users())->order(['id' => 'desc']);
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $address = (new Address())->where('user_id', $result['id'])->order('is_default desc')->find() ?? [];
            if (!empty($address)) {
                $address['province_name'] = (new Region())->where(['area_code' => $address['province'], 'level' => 1])->value('name');
                $address['city_name'] = (new Region())->where(['area_code' => $address['city'], 'level' => 2])->value('name');
                $address['area_name'] = (new Region())->where(['area_code' => $address['area'], 'level' => 3])->value('name');
                $address['street_name'] = (new Region())->where(['id' => $address['street'], 'level' => 4])->value('name');
            }
            $result['level_name'] = (new UserLevel())->where(['level' => $result['level']])->value('name');
            $result['address'] = $address;
        }
        return view(null, [
            'list' => $results
        ]);
    }

    public function coupon()
    {
        $user_id = $this->request->get('user_id/d', 0);
        $results = (new Coupon())->where('user_id', $user_id)->order('endtime desc')->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($results as $result) {
            $result['shop_name'] = '--';
            if ($result['type'] == 2) {
                $result['shop_name'] = (new Business())->where('id', $result['business'])->value('title');
            }
        }
        return view(null, [
            'list' => $results
        ]);
    }

    public function couponel()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id/d', 0);
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择删除项目']);
            }
            $banner = (new Coupon())->where('id', $id)->find();
            if (empty($banner)) {
                return json(['status' => 1, 'msg' => '优惠券不存在，请重试']);
            }
            if ($banner['status'] != 1) {
                return json(['status' => 1, 'msg' => '优惠券已作废']);
            }
            $res = (new Coupon())->where('id', $id)->update(['status' => 2, 'remark' => $banner['remark'] . '|管理员作废']);
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }

    public function sendcoupon()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'type' => 'require|in:1,2',
                'getmoney' => 'require',
                'money' => 'require',
                'user_id' => 'require',
                'days' => 'require',

            ], [
                'type.require' => '类型不能为空',
                'type.in' => '类型不正确',
                'getmoney.require' => '消费金额不能为空',
                'money.require' => '抵扣金额不能为空',
                'user_id.require' => '用户ID不能为空',
                'days.require' => '有效期不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if ($data['type'] == 2 && empty($data['business'])) {
                return json(['status' => 1, 'msg' => '商家不能为空']);
            }
            $data['endtime'] = date('Y-m-d H:i:s', time() + $data['days'] * 86400);
            $res = (new Coupon())->allowfield(true)->save($data);
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 0, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('user_id/d', 0);
            $business = (new Business())->where(['status' => 1])->select();
            return view('', [
                'business' => $business,
                'id' => $id
            ]);
        }
    }
}
